<?php

namespace App\Http\Controllers\Api;

use App\Consts\GlobalConst;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;

class NoteController extends BaseController
{
    public $validate = [
        "add" => ["rules" => [
            "article_id" => "required|string|max:24",
            "content" => "required|string|max:1000",
            "is_public" => "required|in:0,1",
        ]],
        "addLine" => ["rules" => [
            "article_id" => "required|string|max:24",
            "content" => "required|string|max:1000",
            "selected" => "required|string|max:1000",
            "start_index" => "required",
            "end_index" => "required",
            "is_public" => "required|in:0,1",
        ]],
        "articleList" => ["rules" => [
            "article_id" => "required|string|max:24",
            "tab" => "required|string|max:1000",
            "order" => "required|string|max:1000",
            "last_id" => "max:24",
        ]],
        "del" => ["rules" => [
            "id" => "required|string|max:24",
        ]],
        "edit" => ["rules" => [
            "id" => "required|string|max:24",
            "is_public" => "required|in:0,1",
        ]],
        "getDetail" => ["rules" => [
            "id" => "required|string|max:24",
        ]],
        "lineList" => ["rules" => [
            "start_index" => "required",
            "end_index" => "required",
            "order" => "required|string|max:1000",
            "last_id" => "max:24",
        ]],
    ];

    /**
     * @path /note/add
     * @category 笔记
     * @title 新增文章笔记
     * @method POST
     * @params [{"name":"article_id","type":"text","required":1,"example":"","desc":"文章ID"},{"name":"content","type":"text","required":1,"example":"","desc":"笔记内容"},{"name":"is_public","type":"text","required":0,"example":"","desc":"是否公开，0私密，1公开【默认】"}]
     * @resBody {"success":true,"statusCode":200,"result":{"id":"24位笔记ID"},"message":"OK"}
     */
    public function add(Request $request)
    {
        $params = $request->all();

        $data = service()->NoteDataService->onAdd($this->getBaseInfo($params));

        return $this->apiReturn($data);
    }

    /**
     * @path /note/add-line
     * @category 笔记
     * @title 新增划线笔记
     * @method POST
     * @params [{"name":"article_id","type":"text","required":1,"example":"","desc":"文章ID"},{"name":"content","type":"text","required":1,"example":"","desc":"笔记内容"},{"name":"selected","type":"text","required":1,"example":"","desc":"文章节选"},{"name":"start_index","type":"text","required":1,"example":"","desc":"文章节选开始索引"},{"name":"end_index","type":"text","required":1,"example":"","desc":"文章节选结束索引"},{"name":"is_public","type":"text","required":0,"example":"","desc":"是否公开，0私密，1公开【默认】"}]
     * @resBody {"success":true,"statusCode":200,"result":{"id":"24位笔记ID","start_index":"文章节选开始索引","end_index":"文章节选结束索引"},"message":"OK"}
     */
    public function addLine(Request $request)
    {
        $params = $request->all();

        $data = service()->NoteDataService->onAddLine($this->getBaseInfo($params));

        return $this->apiReturn($data);
    }

    /**
     * @path /note/choice-list
     * @category 笔记
     * @title 文章详情 - 笔记列表
     * @method POST
     * @params [{"name":"article_id","type":"text","required":1,"example":"","desc":"文章ID"}]
     * @resBody {"success":true,"statusCode":200,"message":"操作成功","result":{"count":"该文章的笔记总数","have_more":false,"list":[{"id":"24位文章笔记或划线笔记ID","type":"笔记类型，1文章笔记，2划线笔记，3随手记","user_id":"24位用户ID","article_id":"24位文章ID","is_deleted":0,"is_public":1,"created_at":"10位时间戳","start_index":"文章节选开始索引","end_index":"文章节选结束索引","content":"笔记内容","selected":"文章节选内容","is_like":0,"is_collect":1,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像，如1.jpg"},"article":{"id":"24位文章ID","cover_url":"文章封面图，如1.jpg","title":"文章标题","description":"文章描述","is_deleted":0,"is_shield":0,"created_at":"文章创建时间戳"},"count":{"comment":"评论数","like":"点赞数","collect":"收藏数"}},{"id":"24位随手记ID","type":"笔记类型，1文章笔记，2划线笔记，3随手记","user_id":"24位用户ID","article_id":"","is_deleted":0,"is_public":1,"created_at":"10位时间戳","start_index":0,"end_index":0,"content":"笔记内容","selected":"","is_like":0,"is_collect":1,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像，如1.jpg"},"article":{},"topic":[{"id":"话题ID","title":"话题标题"}],"count":{"comment":"评论数","like":"点赞数","collect":"收藏数"}}]}}
     */
    public function choiceList(Request $request)
    {
        $params = $request->all();

        $data = service()->NoteDataService->onChoiceList($this->getBaseInfo($params));

        return $this->apiReturn($data);
    }

    /**
     * @path /note/article-list
     * @category 笔记
     * @title 文章笔记列表
     * @method POST
     * @params [{"name":"article_id","type":"text","required":1,"example":"","desc":"文章ID"},{"name":"tab","type":"text","required":0,"example":"","desc":"我的笔记my【默认】，全部笔记all"},{"name":"order","type":"text","required":0,"example":"","desc":"最新笔记在，前desc【默认】，后asc"},{"name":"last_id","type":"text","required":0,"example":"","desc":"【翻页时必传】当前页最后一条记录的ID"}]
     * @resBody {"success":true,"statusCode":200,"message":"操作成功","result":{"have_more":false,"list":[{"id":"24位文章笔记或划线笔记ID","type":"笔记类型，1文章笔记，2划线笔记，3随手记","user_id":"24位用户ID","article_id":"24位文章ID","is_deleted":0,"is_public":1,"created_at":"10位时间戳","start_index":"文章节选开始索引","end_index":"文章节选结束索引","content":"笔记内容","selected":"文章节选内容","is_like":0,"is_collect":1,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像，如1.jpg"},"article":{"id":"24位文章ID","cover_url":"文章封面图，如1.jpg","title":"文章标题","description":"文章描述","is_deleted":0,"is_shield":0,"created_at":"文章创建时间戳"},"count":{"comment":"评论数","like":"点赞数","collect":"收藏数"}},{"id":"24位随手记ID","type":"笔记类型，1文章笔记，2划线笔记，3随手记","user_id":"24位用户ID","article_id":"","is_deleted":0,"is_public":1,"created_at":"10位时间戳","start_index":0,"end_index":0,"content":"笔记内容","selected":"","is_like":0,"is_collect":1,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像，如1.jpg"},"article":{},"topic":[{"id":"话题ID","title":"话题标题"}],"count":{"comment":"评论数","like":"点赞数","collect":"收藏数"}}]}}
     */
    public function articleList(Request $request)
    {
        $params = $request->all();
        if (empty($params['order'])) {
            $params['order'] = GlobalConst::ORDER_TYPE_DESC;
        }
        $params['order'] = $params['order'] == GlobalConst::ORDER_TYPE_DESC ? SORT_DESC : SORT_ASC;

        $data = service()->NoteDataService->onArticleNoteList($this->getBaseInfo($params));

        return $this->apiReturn($data);
    }

    /**
     * @path /note/line-list
     * @category 笔记
     * @title 文章划线笔记列表
     * @method POST
     * @params [{"name":"article_id","type":"text","required":1,"example":"","desc":"文章ID"},{"name":"start_index","type":"text","required":1,"example":"","desc":"文章节选开始索引"},{"name":"end_index","type":"text","required":1,"example":"","desc":"文章节选结束索引"},{"name":"order","type":"text","required":0,"example":"","desc":"最新笔记在，前desc【默认】，后asc"},{"name":"last_id","type":"text","required":0,"example":"","desc":"【翻页时必传】当前页最后一条记录的ID"}]
     * @resBody {"success":true,"statusCode":200,"message":"操作成功","result":{"have_more":false,"list":[{"id":"24位文章笔记或划线笔记ID","type":"笔记类型，1文章笔记，2划线笔记，3随手记","user_id":"24位用户ID","article_id":"24位文章ID","is_deleted":0,"is_public":1,"created_at":"10位时间戳","start_index":"文章节选开始索引","end_index":"文章节选结束索引","content":"笔记内容","selected":"文章节选内容","is_like":0,"is_collect":1,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像，如1.jpg"},"article":{"id":"24位文章ID","cover_url":"文章封面图，如1.jpg","title":"文章标题","description":"文章描述","is_deleted":0,"is_shield":0,"created_at":"文章创建时间戳"},"count":{"comment":"评论数","like":"点赞数","collect":"收藏数"}},{"id":"24位随手记ID","type":"笔记类型，1文章笔记，2划线笔记，3随手记","user_id":"24位用户ID","article_id":"","is_deleted":0,"is_public":1,"created_at":"10位时间戳","start_index":0,"end_index":0,"content":"笔记内容","selected":"","is_like":0,"is_collect":1,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像，如1.jpg"},"article":{},"topic":[{"id":"话题ID","title":"话题标题"}],"count":{"comment":"评论数","like":"点赞数","collect":"收藏数"}}]}}
     */
    public function lineList(Request $request)
    {
        $params = $request->all();

        $data = service()->NoteDataService->onLineList($this->getBaseInfo($params));

        return $this->apiReturn($data);
    }

    /**
     * @path /note/del
     * @category 笔记
     * @title 删除
     * @method POST
     * @params [{"name":"id","type":"text","required":1,"example":"","desc":"笔记ID"}]
     * @resBody {"success":true,"statusCode":200,"result":true,"message":"OK"}
     */
    public function del(Request $request)
    {
        $params = $request->all();

        $data = service()->NoteDataService->onDel($this->getBaseInfo($params));

        return $this->apiReturn($data);
    }

    /**
     * @path /note/edit
     * @category 笔记
     * @title 修改
     * @method POST
     * @params [{"name":"id","type":"text","required":1,"example":"","desc":"笔记ID"},{"name":"is_public","type":"text","required":0,"example":"","desc":"是否公开，0私密，1公开"}]
     * @resBody {"success":true,"statusCode":200,"result":true,"message":"OK"}
     */
    public function edit(Request $request)
    {
        $params = $request->all();

        $data = service()->NoteDataService->onEdit([
            'id' => $params['id'],
            'current_user_id' => $this->getCurrentUserId(),
            'is_public' => $params['is_public']
        ]);

        return $this->apiReturn($data);
    }

    /**
     * @path /note/get-detail
     * @category 笔记
     * @title 详情
     * @method POST
     * @params [{"name":"id","type":"text","required":1,"example":"","desc":"笔记ID"}]
     * @resBody {"success":true,"statusCode":200,"message":"操作成功","result":{"id":"24位文章笔记或划线笔记ID","type":"笔记类型，1文章笔记，2划线笔记，3随手记","user_id":"24位用户ID","article_id":"24位文章ID","is_deleted":0,"is_public":1,"created_at":"10位时间戳","start_index":"文章节选开始索引","end_index":"文章节选结束索引","content":"笔记内容","selected":"文章节选内容","is_like":0,"is_collect":1,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像，如1.jpg"},"article":{"id":"24位文章ID","cover_url":"文章封面图，如1.jpg","title":"文章标题","description":"文章描述","is_deleted":0,"is_shield":0,"created_at":"文章创建时间戳"},"count":{"comment":"评论数","like":"点赞数","collect":"收藏数"}}}
     */
    public function getDetail(Request $request)
    {
        $params = $request->all();

        $data = service()->NoteDataService->onGetDetail($this->getBaseInfo($params));

        return $this->apiReturn($data);
    }
}
